package com.flycent.hqhs.ui.fragment;


import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.Constants;
import com.flycent.hqhs.R;
import com.flycent.hqhs.ui.BaseActivity;
import com.flycent.hqhs.ui.EvaluationActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author fbl
 *  评价 fragment界面
 */
public class EvaluationFragment extends BaseFragment {

	private ImageView mProImg,isEvaluationImg;
	private TextView mProNameTxt,mProAttributeTxt,mProPriceTxt;
	private Button mSureBut;
	private EditText mCountEdit;
	private RelativeLayout mButtomRelat;

	@Override  
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
			Bundle savedInstanceState) { 

		return inflater.inflate(R.layout.fragement_evaluation, container, false);  
	}  

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		isEvaluationImg = (ImageView) getView().findViewById(R.id.evaluation_is_true_img);
		mProImg = (ImageView) getView().findViewById(R.id.evaluation_fragment_product_image);
		mProNameTxt = (TextView) getView().findViewById(R.id.evaluation_fragment_product_name);
		mProAttributeTxt = (TextView) getView().findViewById(R.id.evaluation_fragment_product_attribute);
		mProPriceTxt = (TextView) getView().findViewById(R.id.evaluation_fragment_product_price);
		mButtomRelat = (RelativeLayout) getView().findViewById(R.id.evaluation_fragment_buttom_relat);

		ImageLoader.getInstance().displayImage(EvaluationActivity.skuImage, mProImg);

		mProNameTxt.setText(EvaluationActivity.skuName);
		mProAttributeTxt.setText(EvaluationActivity.skuAttribute);
		mProPriceTxt.setText("￥"+EvaluationActivity.skuPrice);

		mCountEdit = (EditText) getView().findViewById(R.id.evaluation_fragment_Edit);
		mSureBut = (Button) getView().findViewById(R.id.evaluation_fragment_publish_but);

		if(EvaluationActivity.reviewStatus){
			isEvaluationImg.setVisibility(View.VISIBLE);
			mButtomRelat.setVisibility(View.GONE);
			mCountEdit.setVisibility(View.GONE);
		}else {
			isEvaluationImg.setVisibility(View.GONE);
			mButtomRelat.setVisibility(View.VISIBLE);
			mCountEdit.setVisibility(View.VISIBLE);
		}

		mSureBut.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				final String ss = mCountEdit.getText().toString();
				if(ss == null || "".equals(ss)){
					App.showShortToast(R.string.please_enter_the_evaluation_of_the_content);
					return;
				}
				
				/** 字数不在做限制 */
			/*	if( ss.length() <10 || ss.length()>500){
					App.showShortToast("字数请限制在10~500之间");
					return;
				}*/
				
				((BaseActivity) getActivity()).showLoadingDialog("");
				FinalHttp fh = new FinalHttp();
				AjaxParams params = new AjaxParams();
				params.put("orderItemId", EvaluationActivity.orderId+"");
//				params.put("score", 5+"");
				params.put("content", ss);
				fh.addHeader("X-Ticket", BaseActivity.getUUID(getActivity()));
				fh.post(Constants.IP+"/rest/reviews/review", params, new AjaxCallBack<Object>() {
					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						App.showLog("t=="+t.toString());
						((BaseActivity) getActivity()).dismissLoadingDialog();
						App.showShortToast(R.string.publishing_failure);
					}

					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
						((BaseActivity) getActivity()).dismissLoadingDialog();
						isEvaluationImg.setVisibility(View.VISIBLE);
						mButtomRelat.setVisibility(View.GONE);
						mCountEdit.setVisibility(View.GONE);
						App.showShortToast(R.string.publishing_success);
					}
				});
			}
		});
	}

}
